﻿using EmoneyWeiXin.Filter;
using EmoneyWeiXinBLL;
using EmoneyWeiXinCommon;
using EmoneyWeiXinEntity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace EmoneyWeiXin.Controllers
{
    public class TJBusinessController : Controller
    {
          [LoginAuthorizeAttribute]
        /// <summary>
        /// 订阅统计页面
        /// </summary>
        public ActionResult StatisticsUserScan(int weixinid = 0)
        {
            ViewBag.leftid = int.Parse(Request["leftid"]);
            ViewBag.weixinid = weixinid;
            return View();
        }
          [LoginAuthorizeAttribute]
        //资讯统计
        public ActionResult StatisticsNewsReport(int weixinid = 0)
        {
            ViewBag.leftid = int.Parse(Request["leftid"]);
            ViewBag.weixinid = weixinid;
            return View();
        }
          [LoginAuthorizeAttribute]
        //用户活跃度
        public ActionResult StatisticsYHHuoyuedu(int weixinid = 0)
        {
            ViewBag.leftid = int.Parse(Request["leftid"]);
            ViewBag.weixinid = weixinid;
            return View();
        }
          [LoginAuthorizeAttribute]
        //用户忠诚度
        public ActionResult StatisticsUserLoyalty(int weixinid = 0)
        {
            ViewBag.leftid = int.Parse(Request["leftid"]);
            ViewBag.weixinid = weixinid;
            return View();
        }
          [LoginAuthorizeAttribute]
        //48小时活跃用户统计
        public ActionResult Statistics48HhyUser(int weixinid = 0)
        {
            ViewBag.leftid = int.Parse(Request["leftid"]);
            ViewBag.weixinid = weixinid;
            return View();
        }
          [LoginAuthorizeAttribute]
        //运营统计
        public ActionResult StatisticsMotion(int weixinid = 0)
        {
            ViewBag.leftid = int.Parse(Request["leftid"]);
            ViewBag.weixinid = weixinid;
            return View();
        }


        //运营统计 

        [HttpPost]
        //获取订阅统计列表
        public ActionResult GetStDetailList()
        {

            string[] tradeTypes = { "H", "D", "W", "M" };
            string result = string.Empty;
            int size = int.Parse(Request["size"]);
            if (size == 0 || size > 50) size = 10;
            int index = int.Parse(Request["index"]);
            DateTime startTime = DateTime.Parse(Request["startTime"]);
            DateTime endTime;
            if (string.IsNullOrEmpty(Request["endTime"]))
            {
                endTime = startTime.AddDays(1);
            }
            else
            {
                endTime = DateTime.Parse(Request["endTime"]);
            }

            string tradeType = Request["tradeType"];
            int weixinId = int.Parse(Request["weixinID"]);
            TJ_Business_USerScanBLL bll = new TJ_Business_USerScanBLL();
            int totalcount = 0;
            string whereText = string.Empty;
            if (tradeType == tradeTypes[0] || tradeType == tradeTypes[1]) //如果选的是，按小时，和按天
            {
                whereText = "wexinid='" + weixinId + "'AND TradeType='" + tradeType + "' AND TradeDate>='" + startTime + "' AND TradeDate<='" + endTime + "'";
            }
            else
            {
                int sTime = 0;
                int eTime = 0;
                if (tradeType == tradeTypes[2]) //如果选择的是 按周
                {
                    sTime = WeekofYear(startTime);
                    eTime = WeekofYear(endTime);
                }
                else
                {  //如果选择的是按月
                    sTime = startTime.Month;
                    eTime = endTime.Month;
                }
                whereText = "wexinid='" + weixinId + "'AND TradeType='" + tradeType + "' AND TradeTime>='" + sTime + "' AND TradeTime<='" + eTime + "'";
            }
            List<TJ_Business_USerScanEntity> list = bll.GetUSerScanListForPage(size, index, whereText, out totalcount);

            foreach (TJ_Business_USerScanEntity item in list)
            {
                if (tradeType == tradeTypes[0])
                {
                    item.ItemName = item.TradeDate.Day + " " + item.TradeTime + ":00";
                }
                else
                    if (tradeType == tradeTypes[1])
                    {
                        item.ItemName = item.TradeDate.ToString("MM-dd");
                    }
                    else
                        if (tradeType == tradeTypes[2])
                        {
                            item.ItemName = item.TradeDate.Year + " 第" + item.TradeTime + "周";
                        }
                        else
                        {
                            item.ItemName = item.TradeDate.ToString("yy-MM");
                        }

            }
            int subSum = 0;
            int uSubSum = 0;
            bll.GetSumDataByTimeArea(out subSum, out uSubSum, whereText);

            return Json(new { totalCount = totalcount, data = list, subSum = subSum, unSubSum = uSubSum });
        }

        private int WeekofYear(DateTime dt)
        {
            int result = 0;
            int dayofYear = dt.DayOfYear;
            DayOfWeek dayOfweek = dt.DayOfWeek;
            if (dayOfweek == DayOfWeek.Sunday)
            {
                result = dayofYear / 7;
            }
            else
            {
                result = dayofYear / 7 + 1;
            }
            return result;
        }

        //获取资讯统计列表
        [HttpPost]
        public ActionResult GetNRDetailList()
        {
            int size = int.Parse(Request["size"]);
            if (size == 0 || size > 50) size = 10;
            int index = int.Parse(Request["index"]);

            string startTime = Request["startTime"];
            string endTime = Request["endTime"];
            string weixinId = Request["weixinId"];
            string lanmu = Request["lanmu"];
            string title = Request["title"];
            string orderBy = Request["orderBy"] == null ? "pubDate desc" : Request["orderBy"];
            int totalRc = 0;//总记录数

            string whereText = " weixinid='" + weixinId + "'";
            if (lanmu != "-1" && lanmu != null)
            {
                whereText += " AND MemuType='" + lanmu + "'";
            }
            if (!string.IsNullOrEmpty(startTime))
            {
                whereText += " AND pubDate>='" + StringTools.Text2SafeSql(startTime) + "'";
            }

            if (!string.IsNullOrEmpty(endTime))
            {
                whereText += " AND pubDate<='" + StringTools.Text2SafeSql(endTime) + "'";
            }

            if (!string.IsNullOrEmpty(title))
            {
                whereText += " AND title like '%" + StringTools.Text2SafeSql(title) + "%'";
            }
            TJ_Business_NewsReportBLL bll = new TJ_Business_NewsReportBLL();
            List<VW_VW_TJNewReportEntity> list = bll.GetNRTJDataByPage(size, index, whereText, orderBy, out totalRc);
            string jsonStr = JsonTools.ToJson(new { dataList = list, totalCount = totalRc });
            return Content(jsonStr);
        }


        //获取用户活跃度数据
        [HttpPost]
        public ActionResult GetHYDDataList()
        {
            TJ_Business_YHHuoyueduBLL bll = new TJ_Business_YHHuoyueduBLL();
            string weixinId = Request["weixinId"];
            List<TJ_Business_HuoyueduEntity> list = bll.GetYHhuoyueduData(weixinId);
            string jsonStr = JsonTools.ToJson(new { dataList = list });
            return Content(jsonStr);
        }

        //获取用户忠诚度数据
        [HttpPost]
        public ActionResult GetUserLoyaltyDataList()
        {
            TJ_Business_LoyaltyBLL bll = new TJ_Business_LoyaltyBLL();
            string weixinId = Request["weixinId"];
            List<TJ_Business_LoyaltyEntity> list = bll.GetLoyaltyData(weixinId);
            string jsonStr = JsonTools.ToJson(new { dataList = list });
            return Content(jsonStr);
        }


        //获取48小时活跃用户数据
        public ActionResult Get48HHyData()
        {
            string startTime = Request["startTime"];
            string endTime = Request["endTime"];
            if (string.IsNullOrEmpty(startTime))
            {
                startTime = System.DateTime.Now.AddDays(-30).ToString("yyyy-MM-dd");
            }

            if (string.IsNullOrEmpty(endTime))
            {
                endTime = System.DateTime.Now.ToString("yyyy-MM-dd");
            }

            string weixinId = Request["weixinID"];

            TJ_Business_48HhyUserBLL bll = new TJ_Business_48HhyUserBLL();
            List<TJ_Business_48HhyUserEntity> list = bll.GetDataByTiemSpan(startTime, endTime, weixinId);
            string result = JsonTools.ToJson(new { dataList = list });
            return Content(result);
        }


        public ActionResult GetMotionData()
        {
            //  if(Request["search"]=="1")
            //wType=1资讯，2关键字，3栏目
            string whereText = string.Empty;
            int pageSize = int.Parse(Request["size"]);
            int pageIndex = int.Parse(Request["index"]);
            int research = Request["research"] == "1" ? 1 : 0;
            int weixinId = int.Parse(Request["weixinID"]);
            string startTime = System.DateTime.Now.AddDays(-30).ToString("yyyy-MM-dd");
            string endTime = System.DateTime.Now.ToString("yyyy-MM-dd");
            int wType = int.Parse(Request["wType"]);
            if (!string.IsNullOrEmpty(Request["startTime"]))
            {
                startTime = Request["startTime"];
            }
            if (!string.IsNullOrEmpty(Request["endTime"]))
            {
                endTime = Request["endTime"];
            }


            TJ_Business_MotionDataBLL bll = new TJ_Business_MotionDataBLL();
            int totalCount = 0;
            int sumPv = 0;
            int sumUv = 0;
            List<TJ_Business_MotionDataEntity> lst = bll.GetMotionListForPage(startTime, endTime, research, weixinId, wType, pageSize, pageIndex, out totalCount, out sumPv, out sumUv);
            string jsonStr = JsonTools.ToJson(new { DataList = lst, totalCount = totalCount, SumPv = sumPv, SumUv = sumUv });
            return Content(jsonStr);
        }

    }


}
